package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope  // nacos配置热刷新注解
public class UserController {

    @Autowired
    private UserService userService;

     /*@Value("${pattern.dateformat}")  // 读取nacos中的配置
     private String dateformat;*/

    @Autowired
    private PatternProperties properties;

    @GetMapping("prop")
    public PatternProperties properties(){
        return properties;
    }

    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,
                          @RequestHeader(value = "name", required = false) String name) {
        System.out.println("name: " + name);
        return userService.queryById(id);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/mq/{id}")
    public void updateOrderByMq(@PathVariable("id") String id) {
        System.out.println("订单id: " + id);
        Map<String,Object> map = new HashMap();
        map.put("name","zhang");
        map.put("age",11);
        rabbitTemplate.convertAndSend("topic.user","query.userinfo",map);
    }
}
